install.packages("RCurl")
require(RCurl)

docParams <- "?key=0Au3BUZg3B8qEdFhLUUZCUmNDMlNQcEtPcnNYa0oyMHc&single=true&gid=0&output=csv"
csvDataLocation = paste("https://docs.google.com/spreadsheet/pub",docParams,sep="")
sunCSV <- getURL(csvDataLocation, ssl.verifypeer=FALSE)

sunFrame <- read.csv(textConnection(sunCSV))

hour <- function(x) {
  hrs <- x %/% 100
  return(hrs + (x - 100*hrs)/60)
}

days<-c(31,28,31,30,31,30,31,31,30,31,30,31)

sunrises <- c()
sunsets <- c()
for (i in 1:12) {
  sunrises <- append(sunrises,sapply(sunFrame[,2*i][1:days[i]],hour))
  sunsets<- append(sunsets,(sapply(sunFrame[,2*i+1][1:days[i]],hour)))
}

x <- 1:sum(days)
windows.options(width=15, height=10)
plot(
  c(x,NA,x), c(sunrises[x],NA,sunsets[x]), type='l',
  asp=8, xaxs='i', yaxs='i', ylim=c(0,24),
  col='red', lwd='2',
  axes=FALSE, ann=FALSE, xpd=NA
)


points(sunsets ,type='l',lwd='2',col='blue')
points(sunsets-sunrises,type='l',lwd='2',col='chartreuse3')

monthStarts <- c(1)
for (i in 1:11) {
  monthStarts <- append(monthStarts, tail(monthStarts,1)+days[i])
}


axis(1, at=monthStarts, las=1, lab=paste('01',
                                         c('01','02','03','04','05','06','07','08','09','10','11','12'),sep='.'))
axis(2, las=1, at=3*0:8)

title(main=paste("Saull\u0113kti, saulrieti un dienas ilgums (",cityName,")"), col.main="red")
title(xlab="2012. gads", col.lab=rgb(0,0.5,0))
title(ylab="Viet\u0113jais laiks (UTC+2)", col.lab=rgb(0,0.5,0))
box()
grid(52,24)



